<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import library dependencies
jimport('joomla.plugin.plugin');

class plgSystemSasseo extends JPlugin {
  
	// Constructor
    function plgSystemSasseo(&$subject, $params) {
    	error_reporting(0);
    	$app = JFactory::getApplication();
		if($app->isAdmin()) return;
    	$uri = clone(JURI::getInstance());
		$route =& JSite::getRouter();		
        $query = $route->parse($uri);
	
        $view = $query["view"];
       
        $option = $query["option"];
        
        //load default location
        $locations = $this->loadOneLocation();
        JRequest::setVar("location",$locations->id);
        JRequest::setVar("location_alias",$locations->alias);
        
       	if($option == "com_bakerys")
       	{
	        switch($view)
	        {
	        	case "bakerys";
	        		$location_alias = $query["location_alias"];
	        		$cat_alias = $query["cat_alias"];
	        		$catid = $query["catid"];
	        		$locationid = $query["location"];
	        		if(empty($locationid))
	        			$this->buildLocation($location_alias);
	        		if(empty($catid))	
	        			$this->buildCategory($cat_alias);
	        		break;
	        	case "bakery":
	        		$cat_alias = $query["cat_alias"];
	        		$catid = $query["catid"];
	        		$location_alias = $query["location_alias"];
	        		$locationid = $query["location"];
	        		$item_alias = $query["item_alias"];
	        		$item_id = $query["id"];
	        		if(empty($locationid))
	        			$this->buildLocation($location_alias);
	        		if(empty($catid))	
	        			$this->buildCategory($cat_alias);
	        		if(empty($item_id))	
	        			$this->buildItem($item_alias);
	        		break;	
	        	case "cart":
	        		$location_alias = $query["location_alias"];
	        		$locationid = $query["location"];
	        		
	        		if(empty($locationid))
	        			$this->buildLocation($location_alias);
	        		break;
				case "regisuser":
	        		$location_alias = $query["location_alias"];
	        		$locationid = $query["location"];
	        		
	        		if(empty($locationid))
	        			$this->buildLocation($location_alias);
	        		break;
				case "billaddress":
					$location_alias = $query["location_alias"];
	        		$locationid = $query["location"];
	        		
	        		if(empty($locationid))
	        			$this->buildLocation($location_alias);
					break;
						
	        }
       	}else 
       	{
       		
       	}
        //exit();
		parent::__construct( $subject, $params );
    }
	
    function loadOneLocation()
	{
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select("*")
			->from("#__bakery_locations")
			->where("state = 1");
		$db->setQuery($query,0,1);
		return $db->loadObject();
	}
    
    function buildLocation($alias)
    {
    	if(!empty($alias))
    	{
    		$db = JFactory::getDbo();
    		$query = $db->getQuery(true);
    		$query->select("id")
    			->from("#__bakery_locations")
    			->where("alias = '$alias'");
    		$db->setQuery($query);
    		$id = $db->loadResult();
    		JRequest::setVar("location",$id);	
    	}	
    }
    
	function buildCategory($alias)
    {
    	if(!empty($alias))
    	{
    		$db = JFactory::getDbo();
    		$query = $db->getQuery(true);
    		$query->select("id")
    			->from("#__categories")
    			->where("alias = '$alias'");
    		$db->setQuery($query);
    		$id = $db->loadResult();
    		JRequest::setVar("catid",$id);	
    	}	
    }
    
	function buildItem($alias)
    {
    	if(!empty($alias))
    	{
    		$db = JFactory::getDbo();
    		$query = $db->getQuery(true);
    		$query->select("id")
    			->from("#__bakery_items")
    			->where("alias = '$alias'");
    		$db->setQuery($query);
    		$id = $db->loadResult();
    		JRequest::setVar("id",$id);	
    	}	
    }
}